﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using HslCommunication.BasicFramework;
using HslCommunication.Reflection;
using Newtonsoft.Json.Linq;

namespace HslCommunication.MQTT
{
	/// <summary>
	/// Mqtt协议的辅助类，提供了一些协议相关的基础方法，方便客户端和服务器端一起调用。<br />
	/// The auxiliary class of the Mqtt protocol provides some protocol-related basic methods for the client and server to call together.
	/// </summary>
	// Token: 0x020000C3 RID: 195
	public class MqttHelper
	{
		/// <summary>
		/// 根据数据的总长度，计算出剩余的数据长度信息<br />
		/// According to the total length of the data, calculate the remaining data length information
		/// </summary>
		/// <param name="length">数据的总长度</param>
		/// <returns>计算结果</returns>
		// Token: 0x06000E58 RID: 3672 RVA: 0x0005421C File Offset: 0x0005241C
		public static OperateResult<byte[]> CalculateLengthToMqttLength(int length)
		{
			bool flag = length > 268435455;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.MQTTDataTooLong);
			}
			else
			{
				bool flag2 = length < 128;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(new byte[]
					{
						(byte)length
					});
				}
				else
				{
					bool flag3 = length < 16384;
					if (flag3)
					{
						result = OperateResult.CreateSuccessResult<byte[]>(new byte[]
						{
							(byte)(length % 128 + 128),
							(byte)(length / 128)
						});
					}
					else
					{
						bool flag4 = length < 2097152;
						if (flag4)
						{
							result = OperateResult.CreateSuccessResult<byte[]>(new byte[]
							{
								(byte)(length % 128 + 128),
								(byte)(length / 128 % 128 + 128),
								(byte)(length / 128 / 128)
							});
						}
						else
						{
							result = OperateResult.CreateSuccessResult<byte[]>(new byte[]
							{
								(byte)(length % 128 + 128),
								(byte)(length / 128 % 128 + 128),
								(byte)(length / 128 / 128 % 128 + 128),
								(byte)(length / 128 / 128 / 128)
							});
						}
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 将一个数据打包成一个mqtt协议的内容<br />
		/// Pack a piece of data into a mqtt protocol
		/// </summary>
		/// <param name="control">控制码</param>
		/// <param name="flags">标记</param>
		/// <param name="variableHeader">可变头的字节内容</param>
		/// <param name="payLoad">负载数据</param>
		/// <returns>带有是否成功的结果对象</returns>
		// Token: 0x06000E59 RID: 3673 RVA: 0x00054380 File Offset: 0x00052580
		public static OperateResult<byte[]> BuildMqttCommand(byte control, byte flags, byte[] variableHeader, byte[] payLoad)
		{
			control = (byte)(control << 4);
			byte head = control | flags;
			return MqttHelper.BuildMqttCommand(head, variableHeader, payLoad);
		}

		/// <summary>
		/// 将一个数据打包成一个mqtt协议的内容<br />
		/// Pack a piece of data into a mqtt protocol
		/// </summary>
		/// <param name="head">控制码加标记码</param>
		/// <param name="variableHeader">可变头的字节内容</param>
		/// <param name="payLoad">负载数据</param>
		/// <returns>带有是否成功的结果对象</returns>
		// Token: 0x06000E5A RID: 3674 RVA: 0x000543A8 File Offset: 0x000525A8
		public static OperateResult<byte[]> BuildMqttCommand(byte head, byte[] variableHeader, byte[] payLoad)
		{
			bool flag = variableHeader == null;
			if (flag)
			{
				variableHeader = new byte[0];
			}
			bool flag2 = payLoad == null;
			if (flag2)
			{
				payLoad = new byte[0];
			}
			OperateResult<byte[]> operateResult = MqttHelper.CalculateLengthToMqttLength(variableHeader.Length + payLoad.Length);
			bool flag3 = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag3)
			{
				result = operateResult;
			}
			else
			{
				MemoryStream memoryStream = new MemoryStream();
				memoryStream.WriteByte(head);
				memoryStream.Write(operateResult.Content, 0, operateResult.Content.Length);
				bool flag4 = variableHeader.Length != 0;
				if (flag4)
				{
					memoryStream.Write(variableHeader, 0, variableHeader.Length);
				}
				bool flag5 = payLoad.Length != 0;
				if (flag5)
				{
					memoryStream.Write(payLoad, 0, payLoad.Length);
				}
				byte[] value = memoryStream.ToArray();
				memoryStream.Dispose();
				result = OperateResult.CreateSuccessResult<byte[]>(value);
			}
			return result;
		}

		/// <summary>
		/// 将字符串打包成utf8编码，并且带有2个字节的表示长度的信息<br />
		/// Pack the string into utf8 encoding, and with 2 bytes of length information
		/// </summary>
		/// <param name="message">文本消息</param>
		/// <returns>打包之后的信息</returns>
		// Token: 0x06000E5B RID: 3675 RVA: 0x00054468 File Offset: 0x00052668
		public static byte[] BuildSegCommandByString(string message)
		{
			byte[] array = string.IsNullOrEmpty(message) ? new byte[0] : Encoding.UTF8.GetBytes(message);
			byte[] array2 = new byte[array.Length + 2];
			array.CopyTo(array2, 2);
			array2[0] = (byte)(array.Length / 256);
			array2[1] = (byte)(array.Length % 256);
			return array2;
		}

		/// <summary>
		/// 从MQTT的缓存信息里，提取文本信息<br />
		/// Extract text information from MQTT cache information
		/// </summary>
		/// <param name="buffer">Mqtt的报文</param>
		/// <param name="index">索引</param>
		/// <returns>值</returns>
		// Token: 0x06000E5C RID: 3676 RVA: 0x000544C8 File Offset: 0x000526C8
		public static string ExtraMsgFromBytes(byte[] buffer, ref int index)
		{
			int num = index;
			int num2 = (int)buffer[index] * 256 + (int)buffer[index + 1];
			index = index + 2 + num2;
			return Encoding.UTF8.GetString(buffer, num + 2, num2);
		}

		/// <summary>
		/// 从MQTT的缓存信息里，提取文本信息<br />
		/// Extract text information from MQTT cache information
		/// </summary>
		/// <param name="buffer">Mqtt的报文</param>
		/// <param name="index">索引</param>
		/// <returns>值</returns>
		// Token: 0x06000E5D RID: 3677 RVA: 0x00054508 File Offset: 0x00052708
		public static string ExtraSubscribeMsgFromBytes(byte[] buffer, ref int index)
		{
			int num = index;
			int num2 = (int)buffer[index] * 256 + (int)buffer[index + 1];
			index = index + 3 + num2;
			return Encoding.UTF8.GetString(buffer, num + 2, num2);
		}

		/// <summary>
		/// 从MQTT的缓存信息里，提取长度信息<br />
		/// Extract length information from MQTT cache information
		/// </summary>
		/// <param name="buffer">Mqtt的报文</param>
		/// <param name="index">索引</param>
		/// <returns>值</returns>
		// Token: 0x06000E5E RID: 3678 RVA: 0x00054548 File Offset: 0x00052748
		public static int ExtraIntFromBytes(byte[] buffer, ref int index)
		{
			int result = (int)buffer[index] * 256 + (int)buffer[index + 1];
			index += 2;
			return result;
		}

		/// <summary>
		/// 从MQTT的缓存信息里，提取长度信息<br />
		/// Extract length information from MQTT cache information
		/// </summary>
		/// <param name="data">数据信息</param>
		/// <returns>值</returns>
		// Token: 0x06000E5F RID: 3679 RVA: 0x00054574 File Offset: 0x00052774
		public static byte[] BuildIntBytes(int data)
		{
			return new byte[]
			{
				BitConverter.GetBytes(data)[1],
				BitConverter.GetBytes(data)[0]
			};
		}

		/// <summary>
		/// 创建MQTT连接服务器的报文信息<br />
		/// Create MQTT connection server message information
		/// </summary>
		/// <param name="connectionOptions">连接配置</param>
		/// <param name="protocol">协议的内容</param>
		/// <returns>返回是否成功的信息</returns>
		// Token: 0x06000E60 RID: 3680 RVA: 0x000545A4 File Offset: 0x000527A4
		public static OperateResult<byte[]> BuildConnectMqttCommand(MqttConnectionOptions connectionOptions, string protocol = "MQTT")
		{
			List<byte> list = new List<byte>();
			list.AddRange(new byte[]
			{
				0,
				4
			});
			list.AddRange(Encoding.ASCII.GetBytes(protocol));
			list.Add(4);
			byte b = 0;
			bool flag = connectionOptions.Credentials != null;
			if (flag)
			{
				b |= 128;
				b |= 64;
			}
			bool cleanSession = connectionOptions.CleanSession;
			if (cleanSession)
			{
				b |= 2;
			}
			list.Add(b);
			bool flag2 = connectionOptions.KeepAlivePeriod.TotalSeconds < 1.0;
			if (flag2)
			{
				connectionOptions.KeepAlivePeriod = TimeSpan.FromSeconds(1.0);
			}
			byte[] bytes = BitConverter.GetBytes((int)connectionOptions.KeepAlivePeriod.TotalSeconds);
			list.Add(bytes[1]);
			list.Add(bytes[0]);
			List<byte> list2 = new List<byte>();
			list2.AddRange(MqttHelper.BuildSegCommandByString(connectionOptions.ClientId));
			bool flag3 = connectionOptions.Credentials != null;
			if (flag3)
			{
				list2.AddRange(MqttHelper.BuildSegCommandByString(connectionOptions.Credentials.UserName));
				list2.AddRange(MqttHelper.BuildSegCommandByString(connectionOptions.Credentials.Password));
			}
			return MqttHelper.BuildMqttCommand(1, 0, list.ToArray(), list2.ToArray());
		}

		/// <summary>
		/// 根据服务器返回的信息判断当前的连接是否是可用的<br />
		/// According to the information returned by the server to determine whether the current connection is available
		/// </summary>
		/// <param name="code">功能码</param>
		/// <param name="data">数据内容</param>
		/// <returns>是否可用的连接</returns>
		// Token: 0x06000E61 RID: 3681 RVA: 0x000546F0 File Offset: 0x000528F0
		public static OperateResult CheckConnectBack(byte code, byte[] data)
		{
			bool flag = code >> 4 != 2;
			OperateResult result;
			if (flag)
			{
				result = new OperateResult("MQTT Connection Back Is Wrong: " + code.ToString());
			}
			else
			{
				bool flag2 = data.Length < 2;
				if (flag2)
				{
					result = new OperateResult("MQTT Connection Data Is Short: " + SoftBasic.ByteToHexString(data, ' '));
				}
				else
				{
					int num = (int)data[1];
					int num2 = (int)data[0];
					bool flag3 = num > 0;
					if (flag3)
					{
						result = new OperateResult(num, MqttHelper.GetMqttCodeText(num));
					}
					else
					{
						result = OperateResult.CreateSuccessResult();
					}
				}
			}
			return result;
		}

		/// <summary>
		/// 获取当前的错误的描述信息<br />
		/// Get a description of the current error
		/// </summary>
		/// <param name="status">状态信息</param>
		/// <returns>描述信息</returns>
		// Token: 0x06000E62 RID: 3682 RVA: 0x00054774 File Offset: 0x00052974
		public static string GetMqttCodeText(int status)
		{
			string result;
			switch (status)
			{
			case 1:
				result = StringResources.Language.MQTTStatus01;
				break;
			case 2:
				result = StringResources.Language.MQTTStatus02;
				break;
			case 3:
				result = StringResources.Language.MQTTStatus03;
				break;
			case 4:
				result = StringResources.Language.MQTTStatus04;
				break;
			case 5:
				result = StringResources.Language.MQTTStatus05;
				break;
			default:
				result = StringResources.Language.UnknownError;
				break;
			}
			return result;
		}

		/// <summary>
		/// 创建Mqtt发送消息的命令<br />
		/// Create Mqtt command to send messages
		/// </summary>
		/// <param name="message">封装后的消息内容</param>
		/// <returns>结果内容</returns>
		// Token: 0x06000E63 RID: 3683 RVA: 0x000547F4 File Offset: 0x000529F4
		public static OperateResult<byte[]> BuildPublishMqttCommand(MqttPublishMessage message)
		{
			byte b = 0;
			bool flag = !message.IsSendFirstTime;
			if (flag)
			{
				b |= 8;
			}
			bool retain = message.Message.Retain;
			if (retain)
			{
				b |= 1;
			}
			bool flag2 = message.Message.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce;
			if (flag2)
			{
				b |= 2;
			}
			else
			{
				bool flag3 = message.Message.QualityOfServiceLevel == MqttQualityOfServiceLevel.ExactlyOnce;
				if (flag3)
				{
					b |= 4;
				}
				else
				{
					bool flag4 = message.Message.QualityOfServiceLevel == MqttQualityOfServiceLevel.OnlyTransfer;
					if (flag4)
					{
						b |= 6;
					}
				}
			}
			List<byte> list = new List<byte>();
			list.AddRange(MqttHelper.BuildSegCommandByString(message.Message.Topic));
			bool flag5 = message.Message.QualityOfServiceLevel > MqttQualityOfServiceLevel.AtMostOnce;
			if (flag5)
			{
				list.Add(BitConverter.GetBytes(message.Identifier)[1]);
				list.Add(BitConverter.GetBytes(message.Identifier)[0]);
			}
			return MqttHelper.BuildMqttCommand(3, b, list.ToArray(), message.Message.Payload);
		}

		/// <summary>
		/// 创建Mqtt发送消息的命令<br />
		/// Create Mqtt command to send messages
		/// </summary>
		/// <param name="topic">主题消息内容</param>
		/// <param name="payload">数据负载</param>
		/// <returns>结果内容</returns>
		// Token: 0x06000E64 RID: 3684 RVA: 0x000548F4 File Offset: 0x00052AF4
		public static OperateResult<byte[]> BuildPublishMqttCommand(string topic, byte[] payload)
		{
			return MqttHelper.BuildMqttCommand(3, 0, MqttHelper.BuildSegCommandByString(topic), payload);
		}

		/// <summary>
		/// 创建Mqtt订阅消息的命令<br />
		/// Command to create Mqtt subscription message
		/// </summary>
		/// <param name="message">订阅的主题</param>
		/// <returns>结果内容</returns>
		// Token: 0x06000E65 RID: 3685 RVA: 0x00054914 File Offset: 0x00052B14
		public static OperateResult<byte[]> BuildSubscribeMqttCommand(MqttSubscribeMessage message)
		{
			List<byte> list = new List<byte>();
			List<byte> list2 = new List<byte>();
			list.Add(BitConverter.GetBytes(message.Identifier)[1]);
			list.Add(BitConverter.GetBytes(message.Identifier)[0]);
			for (int i = 0; i < message.Topics.Length; i++)
			{
				list2.AddRange(MqttHelper.BuildSegCommandByString(message.Topics[i]));
				bool flag = message.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtMostOnce;
				if (flag)
				{
					list2.AddRange(new byte[1]);
				}
				else
				{
					bool flag2 = message.QualityOfServiceLevel == MqttQualityOfServiceLevel.AtLeastOnce;
					if (flag2)
					{
						list2.AddRange(new byte[]
						{
							1
						});
					}
					else
					{
						list2.AddRange(new byte[]
						{
							2
						});
					}
				}
			}
			return MqttHelper.BuildMqttCommand(8, 2, list.ToArray(), list2.ToArray());
		}

		/// <summary>
		/// 创建Mqtt取消订阅消息的命令<br />
		/// Create Mqtt unsubscribe message command
		/// </summary>
		/// <param name="message">订阅的主题</param>
		/// <returns>结果内容</returns>
		// Token: 0x06000E66 RID: 3686 RVA: 0x000549F0 File Offset: 0x00052BF0
		public static OperateResult<byte[]> BuildUnSubscribeMqttCommand(MqttSubscribeMessage message)
		{
			List<byte> list = new List<byte>();
			List<byte> list2 = new List<byte>();
			list.Add(BitConverter.GetBytes(message.Identifier)[1]);
			list.Add(BitConverter.GetBytes(message.Identifier)[0]);
			for (int i = 0; i < message.Topics.Length; i++)
			{
				list2.AddRange(MqttHelper.BuildSegCommandByString(message.Topics[i]));
			}
			return MqttHelper.BuildMqttCommand(10, 2, list.ToArray(), list2.ToArray());
		}

		/// <summary>
		/// 解析从MQTT接受的客户端信息，解析成实际的Topic数据及Payload数据<br />
		/// Parse the client information received from MQTT and parse it into actual Topic data and Payload data
		/// </summary>
		/// <param name="mqttCode">MQTT的命令码</param>
		/// <param name="data">接收的MQTT原始的消息内容</param>
		/// <returns>解析的数据结果信息</returns>
		// Token: 0x06000E67 RID: 3687 RVA: 0x00054A78 File Offset: 0x00052C78
		public static OperateResult<string, byte[]> ExtraMqttReceiveData(byte mqttCode, byte[] data)
		{
			bool flag = data.Length < 2;
			OperateResult<string, byte[]> result;
			if (flag)
			{
				result = new OperateResult<string, byte[]>(StringResources.Language.ReceiveDataLengthTooShort + data.Length.ToString());
			}
			else
			{
				int num = (int)data[0] * 256 + (int)data[1];
				bool flag2 = data.Length < 2 + num;
				if (flag2)
				{
					result = new OperateResult<string, byte[]>(string.Format("Code[{0:X2}] ExtraMqttReceiveData Error: {1}", mqttCode, SoftBasic.ByteToHexString(data, ' ')));
				}
				else
				{
					string value = (num > 0) ? Encoding.UTF8.GetString(data, 2, num) : string.Empty;
					byte[] array = new byte[data.Length - num - 2];
					Array.Copy(data, num + 2, array, 0, array.Length);
					result = OperateResult.CreateSuccessResult<string, byte[]>(value, array);
				}
			}
			return result;
		}

		/// <summary>
		/// 使用指定的对象来返回网络的API接口，前提是传入的数据为json参数，返回的数据为json数据，详细参照说明<br />
		/// Use the specified object to return the API interface of the network, 
		/// provided that the incoming data is json parameters and the returned data is json data, 
		/// please refer to the description for details
		/// </summary>
		/// <param name="mqttSession">当前的对话状态</param>
		/// <param name="message">当前传入的消息内容</param>
		/// <param name="obj">等待解析的api解析的对象</param>
		/// <returns>等待返回客户的结果</returns>
		// Token: 0x06000E68 RID: 3688 RVA: 0x00054B34 File Offset: 0x00052D34
		public static OperateResult<string> HandleObjectMethod(MqttSession mqttSession, MqttClientApplicationMessage message, object obj)
		{
			string text = message.Topic;
			bool flag = text.LastIndexOf('/') >= 0;
			if (flag)
			{
				text = text.Substring(text.LastIndexOf('/') + 1);
			}
			MethodInfo method = obj.GetType().GetMethod(text);
			bool flag2 = method == null;
			OperateResult<string> result;
			if (flag2)
			{
				result = new OperateResult<string>("Current MqttSync Api ：[" + text + "] not exsist");
			}
			else
			{
				OperateResult<MqttRpcApiInfo> mqttSyncServicesApiFromMethod = MqttHelper.GetMqttSyncServicesApiFromMethod("", method, obj, null);
				bool flag3 = !mqttSyncServicesApiFromMethod.IsSuccess;
				if (flag3)
				{
					result = OperateResult.CreateFailedResult<string>(mqttSyncServicesApiFromMethod);
				}
				else
				{
					result = MqttHelper.HandleObjectMethod(mqttSession, message, mqttSyncServicesApiFromMethod.Content);
				}
			}
			return result;
		}

		/// <summary>
		/// 使用指定的对象来返回网络的API接口，前提是传入的数据为json参数，返回的数据为json数据，详细参照说明<br />
		/// Use the specified object to return the API interface of the network, 
		/// provided that the incoming data is json parameters and the returned data is json data, 
		/// please refer to the description for details
		/// </summary>
		/// <param name="mqttSession">当前的对话状态</param>
		/// <param name="message">当前传入的消息内容</param>
		/// <param name="apiInformation">当前已经解析好的Api内容对象</param>
		/// <returns>等待返回客户的结果</returns>
		// Token: 0x06000E69 RID: 3689 RVA: 0x00054BDC File Offset: 0x00052DDC
		public static OperateResult<string> HandleObjectMethod(MqttSession mqttSession, MqttClientApplicationMessage message, MqttRpcApiInfo apiInformation)
		{
			object obj = null;
			bool flag = apiInformation.PermissionAttribute != null;
			if (flag)
			{
				bool flag2 = Authorization.asdniasnfaksndiqwhawfskhfaiw();
				if (!flag2)
				{
					return new OperateResult<string>("Permission function need authorization ：" + StringResources.Language.InsufficientPrivileges);
				}
				bool flag3 = !apiInformation.PermissionAttribute.CheckClientID(mqttSession.ClientId);
				if (flag3)
				{
					return new OperateResult<string>(string.Concat(new string[]
					{
						"Mqtt RPC Api ：[",
						apiInformation.ApiTopic,
						"] Check ClientID[",
						mqttSession.ClientId,
						"] failed, access not permission"
					}));
				}
				bool flag4 = !apiInformation.PermissionAttribute.CheckUserName(mqttSession.UserName);
				if (flag4)
				{
					return new OperateResult<string>(string.Concat(new string[]
					{
						"Mqtt RPC Api ：[",
						apiInformation.ApiTopic,
						"] Check Username[",
						mqttSession.UserName,
						"] failed, access not permission"
					}));
				}
			}
			try
			{
				bool flag5 = apiInformation.Method != null;
				if (flag5)
				{
					string @string = Encoding.UTF8.GetString(message.Payload);
					JObject jobject = string.IsNullOrEmpty(@string) ? new JObject() : JObject.Parse(@string);
					object[] parametersFromJson = HslReflectionHelper.GetParametersFromJson(apiInformation.Method.GetParameters(), @string);
					obj = apiInformation.Method.Invoke(apiInformation.SourceObject, parametersFromJson);
				}
				else
				{
					bool flag6 = apiInformation.Property != null;
					if (flag6)
					{
						obj = apiInformation.Property.GetValue(apiInformation.SourceObject, null);
					}
				}
			}
			catch (Exception ex)
			{
				return new OperateResult<string>("Mqtt RPC Api ：[" + apiInformation.ApiTopic + "] Wrong，Reason：" + ex.Message);
			}
			return HslReflectionHelper.GetOperateResultJsonFromObj(obj);
		}

		/// <inheritdoc cref="M:HslCommunication.MQTT.MqttHelper.GetSyncServicesApiInformationFromObject(System.String,System.Object,HslCommunication.Reflection.HslMqttPermissionAttribute)" />
		// Token: 0x06000E6A RID: 3690 RVA: 0x00054DC0 File Offset: 0x00052FC0
		public static List<MqttRpcApiInfo> GetSyncServicesApiInformationFromObject(object obj)
		{
			Type type = obj as Type;
			bool flag = type != null;
			List<MqttRpcApiInfo> syncServicesApiInformationFromObject;
			if (flag)
			{
				syncServicesApiInformationFromObject = MqttHelper.GetSyncServicesApiInformationFromObject(type.Name, type, null);
			}
			else
			{
				syncServicesApiInformationFromObject = MqttHelper.GetSyncServicesApiInformationFromObject(obj.GetType().Name, obj, null);
			}
			return syncServicesApiInformationFromObject;
		}

		/// <summary>
		/// 根据当前的对象定义的方法信息，获取到所有支持ApiTopic的方法列表信息，包含API名称，示例参数数据，描述信息。<br />
		/// According to the method information defined by the current object, the list information of all methods that support ApiTopic is obtained, 
		/// including the API name, sample parameter data, and description information.
		/// </summary>
		/// <param name="api">指定的ApiTopic的前缀，可以理解为控制器，如果为空，就不携带控制器。</param>
		/// <param name="obj">实际的等待解析的对象</param>
		/// <param name="permissionAttribute">默认的权限特性</param>
		/// <returns>返回所有API说明的列表，类型为<see cref="T:HslCommunication.MQTT.MqttRpcApiInfo" /></returns>
		// Token: 0x06000E6B RID: 3691 RVA: 0x00054E04 File Offset: 0x00053004
		public static List<MqttRpcApiInfo> GetSyncServicesApiInformationFromObject(string api, object obj, HslMqttPermissionAttribute permissionAttribute = null)
		{
			Type type = obj as Type;
			bool flag = type != null;
			Type type2;
			if (flag)
			{
				type2 = type;
				obj = null;
			}
			else
			{
				type2 = obj.GetType();
			}
			MethodInfo[] methods = type2.GetMethods();
			List<MqttRpcApiInfo> list = new List<MqttRpcApiInfo>();
			foreach (MethodInfo method in methods)
			{
				OperateResult<MqttRpcApiInfo> mqttSyncServicesApiFromMethod = MqttHelper.GetMqttSyncServicesApiFromMethod(api, method, obj, permissionAttribute);
				bool flag2 = !mqttSyncServicesApiFromMethod.IsSuccess;
				if (!flag2)
				{
					list.Add(mqttSyncServicesApiFromMethod.Content);
				}
			}
			PropertyInfo[] properties = type2.GetProperties();
			foreach (PropertyInfo propertyInfo in properties)
			{
				OperateResult<HslMqttApiAttribute, MqttRpcApiInfo> mqttSyncServicesApiFromProperty = MqttHelper.GetMqttSyncServicesApiFromProperty(api, propertyInfo, obj, permissionAttribute);
				bool flag3 = !mqttSyncServicesApiFromProperty.IsSuccess;
				if (!flag3)
				{
					bool flag4 = !mqttSyncServicesApiFromProperty.Content1.PropertyUnfold;
					if (flag4)
					{
						list.Add(mqttSyncServicesApiFromProperty.Content2);
					}
					else
					{
						bool flag5 = propertyInfo.GetValue(obj, null) == null;
						if (!flag5)
						{
							List<MqttRpcApiInfo> syncServicesApiInformationFromObject = MqttHelper.GetSyncServicesApiInformationFromObject(mqttSyncServicesApiFromProperty.Content2.ApiTopic, propertyInfo.GetValue(obj, null), permissionAttribute);
							list.AddRange(syncServicesApiInformationFromObject);
						}
					}
				}
			}
			return list;
		}

		/// <summary>
		/// 根据当前的方法的委托信息和类对象，生成<see cref="T:HslCommunication.MQTT.MqttRpcApiInfo" />的API对象信息。
		/// </summary>
		/// <param name="api">Api头信息</param>
		/// <param name="method">方法的委托</param>
		/// <param name="obj">当前注册的API的源对象</param>
		/// <param name="permissionAttribute">默认的权限特性</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000E6C RID: 3692 RVA: 0x00054F48 File Offset: 0x00053148
		public static OperateResult<MqttRpcApiInfo> GetMqttSyncServicesApiFromMethod(string api, MethodInfo method, object obj, HslMqttPermissionAttribute permissionAttribute = null)
		{
			object[] customAttributes = method.GetCustomAttributes(typeof(HslMqttApiAttribute), false);
			bool flag = customAttributes == null || customAttributes.Length == 0;
			OperateResult<MqttRpcApiInfo> result;
			if (flag)
			{
				result = new OperateResult<MqttRpcApiInfo>(string.Format("Current Api ：[{0}] not support Api attribute", method));
			}
			else
			{
				HslMqttApiAttribute hslMqttApiAttribute = (HslMqttApiAttribute)customAttributes[0];
				MqttRpcApiInfo mqttRpcApiInfo = new MqttRpcApiInfo();
				mqttRpcApiInfo.SourceObject = obj;
				mqttRpcApiInfo.Method = method;
				mqttRpcApiInfo.Description = hslMqttApiAttribute.Description;
				mqttRpcApiInfo.HttpMethod = hslMqttApiAttribute.HttpMethod.ToUpper();
				bool flag2 = string.IsNullOrEmpty(hslMqttApiAttribute.ApiTopic);
				if (flag2)
				{
					hslMqttApiAttribute.ApiTopic = method.Name;
				}
				bool flag3 = permissionAttribute == null;
				if (flag3)
				{
					customAttributes = method.GetCustomAttributes(typeof(HslMqttPermissionAttribute), false);
					bool flag4 = customAttributes != null && customAttributes.Length != 0;
					if (flag4)
					{
						mqttRpcApiInfo.PermissionAttribute = (HslMqttPermissionAttribute)customAttributes[0];
					}
				}
				else
				{
					mqttRpcApiInfo.PermissionAttribute = permissionAttribute;
				}
				bool flag5 = string.IsNullOrEmpty(api);
				if (flag5)
				{
					mqttRpcApiInfo.ApiTopic = hslMqttApiAttribute.ApiTopic;
				}
				else
				{
					mqttRpcApiInfo.ApiTopic = api + "/" + hslMqttApiAttribute.ApiTopic;
				}
				mqttRpcApiInfo.ExamplePayload = HslReflectionHelper.GetParametersFromJson(method).ToString();
				result = OperateResult.CreateSuccessResult<MqttRpcApiInfo>(mqttRpcApiInfo);
			}
			return result;
		}

		/// <summary>
		/// 根据当前的方法的委托信息和类对象，生成<see cref="T:HslCommunication.MQTT.MqttRpcApiInfo" />的API对象信息。
		/// </summary>
		/// <param name="api">Api头信息</param>
		/// <param name="property">方法的委托</param>
		/// <param name="obj">当前注册的API的源对象</param>
		/// <param name="permissionAttribute">默认的权限特性</param>
		/// <returns>返回是否成功的结果对象</returns>
		// Token: 0x06000E6D RID: 3693 RVA: 0x00055088 File Offset: 0x00053288
		public static OperateResult<HslMqttApiAttribute, MqttRpcApiInfo> GetMqttSyncServicesApiFromProperty(string api, PropertyInfo property, object obj, HslMqttPermissionAttribute permissionAttribute = null)
		{
			object[] customAttributes = property.GetCustomAttributes(typeof(HslMqttApiAttribute), false);
			bool flag = customAttributes == null || customAttributes.Length == 0;
			OperateResult<HslMqttApiAttribute, MqttRpcApiInfo> result;
			if (flag)
			{
				result = new OperateResult<HslMqttApiAttribute, MqttRpcApiInfo>(string.Format("Current Api ：[{0}] not support Api attribute", property));
			}
			else
			{
				HslMqttApiAttribute hslMqttApiAttribute = (HslMqttApiAttribute)customAttributes[0];
				MqttRpcApiInfo mqttRpcApiInfo = new MqttRpcApiInfo();
				mqttRpcApiInfo.SourceObject = obj;
				mqttRpcApiInfo.Property = property;
				mqttRpcApiInfo.Description = hslMqttApiAttribute.Description;
				mqttRpcApiInfo.HttpMethod = hslMqttApiAttribute.HttpMethod.ToUpper();
				bool flag2 = string.IsNullOrEmpty(hslMqttApiAttribute.ApiTopic);
				if (flag2)
				{
					hslMqttApiAttribute.ApiTopic = property.Name;
				}
				bool flag3 = permissionAttribute == null;
				if (flag3)
				{
					customAttributes = property.GetCustomAttributes(typeof(HslMqttPermissionAttribute), false);
					bool flag4 = customAttributes != null && customAttributes.Length != 0;
					if (flag4)
					{
						mqttRpcApiInfo.PermissionAttribute = (HslMqttPermissionAttribute)customAttributes[0];
					}
				}
				else
				{
					mqttRpcApiInfo.PermissionAttribute = permissionAttribute;
				}
				bool flag5 = string.IsNullOrEmpty(api);
				if (flag5)
				{
					mqttRpcApiInfo.ApiTopic = hslMqttApiAttribute.ApiTopic;
				}
				else
				{
					mqttRpcApiInfo.ApiTopic = api + "/" + hslMqttApiAttribute.ApiTopic;
				}
				mqttRpcApiInfo.ExamplePayload = string.Empty;
				result = OperateResult.CreateSuccessResult<HslMqttApiAttribute, MqttRpcApiInfo>(hslMqttApiAttribute, mqttRpcApiInfo);
			}
			return result;
		}
	}
}
